package com.tebiecloud.base.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.base.client.model.entity.BaseApp;
import com.tebiecloud.base.server.service.BaseAppService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 系统应用-基础信息 前端控制器
 * @author tebie
 * @date 2019-08-02
 */
@RestController
@RequestMapping("baseapp")
public class BaseAppController {

    @Autowired
    private BaseAppService baseAppService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "/list")
    public ResultBody<IPage<BaseApp>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        BaseApp query = pageParams.mapToObject(BaseApp.class);
        QueryWrapper<BaseApp> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(baseAppService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("/get")
    public ResultBody<BaseApp> get(@RequestParam("id") Long id) {
        BaseApp entity = baseAppService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param apiKey API访问key,
     * @param secretKey API访问密钥,
     * @param appName app名称,
     * @param appNameEn app英文名称,
     * @param appIcon 应用图标,
     * @param appType app类型:server-服务应用 app-手机应用 pc-PC网页应用 wap-手机网页应用,
     * @param appDesc app描述,
     * @param appOs 移动应用操作系统:ios-苹果 android-安卓,
     * @param website 官网地址,
     * @param developerId 开发者ID:默认为0,
     * @param status 状态:0-无效 1-有效,
     * @param isPersist 保留数据0-否 1-是 不允许删除
     * @return
     */
    @PostMapping("/add")
    public ResultBody add(
        @RequestParam(value = "apiKey", required = false) String apiKey,
        @RequestParam(value = "secretKey", required = false) String secretKey,
        @RequestParam(value = "appName", required = false) String appName,
        @RequestParam(value = "appNameEn", required = false) String appNameEn,
        @RequestParam(value = "appIcon", required = false) String appIcon,
        @RequestParam(value = "appType", required = false) String appType,
        @RequestParam(value = "appDesc", required = false) String appDesc,
        @RequestParam(value = "appOs", required = false) String appOs,
        @RequestParam(value = "website", required = false) String website,
        @RequestParam(value = "developerId", required = false) Long developerId,
        @RequestParam(value = "status", required = false) Integer status,
        @RequestParam(value = "isPersist", required = false) Integer isPersist
    ) {
        BaseApp entity = new BaseApp();
        entity.setApiKey(apiKey);
        entity.setSecretKey(secretKey);
        entity.setAppName(appName);
        entity.setAppNameEn(appNameEn);
        entity.setAppIcon(appIcon);
        entity.setAppType(appType);
        entity.setAppDesc(appDesc);
        entity.setAppOs(appOs);
        entity.setWebsite(website);
        entity.setDeveloperId(developerId);
        entity.setStatus(status);
        entity.setIsPersist(isPersist);
        baseAppService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param apiKey API访问key
     * @param secretKey API访问密钥
     * @param appName app名称
     * @param appNameEn app英文名称
     * @param appIcon 应用图标
     * @param appType app类型:server-服务应用 app-手机应用 pc-PC网页应用 wap-手机网页应用
     * @param appDesc app描述
     * @param appOs 移动应用操作系统:ios-苹果 android-安卓
     * @param website 官网地址
     * @param developerId 开发者ID:默认为0
     * @param status 状态:0-无效 1-有效
     * @param isPersist 保留数据0-否 1-是 不允许删除
     * @return
     */
    @PostMapping("/update")
    public ResultBody update(
            @RequestParam(value = "appId", required = false) String appId,
            @RequestParam(value = "apiKey", required = false) String apiKey,
            @RequestParam(value = "secretKey", required = false) String secretKey,
            @RequestParam(value = "appName", required = false) String appName,
            @RequestParam(value = "appNameEn", required = false) String appNameEn,
            @RequestParam(value = "appIcon", required = false) String appIcon,
            @RequestParam(value = "appType", required = false) String appType,
            @RequestParam(value = "appDesc", required = false) String appDesc,
            @RequestParam(value = "appOs", required = false) String appOs,
            @RequestParam(value = "website", required = false) String website,
            @RequestParam(value = "developerId", required = false) Long developerId,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "isPersist", required = false) Integer isPersist
    ) {
        BaseApp entity = new BaseApp();
        entity.setAppId(appId);
        entity.setApiKey(apiKey);
        entity.setSecretKey(secretKey);
        entity.setAppName(appName);
        entity.setAppNameEn(appNameEn);
        entity.setAppIcon(appIcon);
        entity.setAppType(appType);
        entity.setAppDesc(appDesc);
        entity.setAppOs(appOs);
        entity.setWebsite(website);
        entity.setDeveloperId(developerId);
        entity.setStatus(status);
        entity.setIsPersist(isPersist);
        baseAppService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("/remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            baseAppService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            baseAppService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
